home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6008 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.3 KB

  1. Path: newbridge.com!gminer
  2. From: gminer@Newbridge.COM (Glen Miner)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: What's better & why
  5. Date: 21 Feb 1996 12:57:42 GMT
  6. Organization: Newbridge Networks Corporation
  7. Message-ID: <4gf4s6$fl0@kannews.ca.newbridge.com>
  8. References: <31297C5A.E6C@connix.com>
  9. NNTP-Posting-Host: thor.ca.newbridge.com
  10.  
  11. >I was just curious what you all though about the following code.
  12. >Please tell me what is better (faster/Smaller) and why. Or does it make 
  13. >any difference at all. I just though about this while I was programming 
  14. >today?
  15. >
  16. >example 1:
  17. >                val = 1;
  18. >                if( what ever...)val = 0;
  19. >
  20. >or
  21. >example 2:
  22. >                if( what ever... )val = 0;
  23. >                else val = 1;
  24.  
  25. What an interesting question :) I'm going to go home, code this and then 
  26. decompile it into ASM to see if it does it this way (my compiler may be 
  27. smart, though).
  28.  
  29. If the comiler was dumb, you would get something like the following ASM 
  30. instructions:
  31.  
  32. example 1:
  33.         mov val, 1
  34.         cmp what, ever
  35.         jne End
  36.         mov val, 0
  37.     End:
  38.  
  39. 2 memory moves, 1 compare, one conditional jump. Fewer instructions = 
  40. smaller code. 3 or 4 instructions executed.
  41.  
  42. example 2:
  43.         cmp what, ever
  44.         jne End
  45.         mov val, 0
  46.         jmp Finish
  47.     End:    mov val 1
  48.     Finish:
  49.  
  50. 2 memory moves, 1 compare, one conditional jump, on unconditional jump.
  51.  
  52. Slightly larger code, one instruction, wee :) Again, 3 or 4 instrucitons 
  53. executed.
  54.  
  55. But: with the worst case in example 1, you are executing an extra move, 
  56. and with the worst case in example 2, you are executing an extra 
  57. unconditional jump. Which is faster? I don't have my ASM handbook here so 
  58. I couldn't tell you ;) If I had to guess, I'd say the jmp would be faster 
  59. becuase your val data wouldn't be registers and all.
  60.  
  61. So, if I wanted speed, like I usually do, and I was willing to pay the 
  62. extra few bytes in code size, I'd stay with example 2.
  63.  
  64. Now, I'm no asm genious, and I don't know how smart my compiler is. It 
  65. may well realize what you are doing and do things an even better way.
  66.  
  67. I'm still tempted to go home and code it in c, and see what the asm turns 
  68. out to be :)_
  69.  
  70. Thanx for the interesting question :)
  71.  
  72. Peace
  73. -- 
  74. Peace
  75. _______________________________________________________________________________
  76. Wonko the Sane: gaminer@undergrad.math.uwaterloo.ca -> Peace.Love.Unity.Respect
  77.